const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    loading: 1
  },

  onLoad: function(options) {
    console.log(options)
    let identity = app.globalData.identityId
    this.setData({
      common: identity == 11 ? 1 : 0
    })
    if (this.data.common == 0) {
      this.getMeetingList()
      this.setData({
        windowHeight: app.globalData.windowHeight - 120
      })
    } else {
      this.getSignInRecord()
      this.setData({
        windowHeight: app.globalData.windowHeight - 240
      })
    }
  },
  onShow: function(options) {
    this.onLoad()
  },

  qrScan: util.throttle(function() {
    util.scanQRCode()
  }),

  // 查询已创建的会议
  getMeetingList() {
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getMeetingInfo',
      method: 'POST',
      data: {
        manager: app.globalData.userId
      },
      dataType: 'json',
      success: res => {
        this.setData({
          meetingListMa: res.data
        })
      },
      fail: res => {
        wx.showToast({
          title: '网络异常！',
          image: '/images/warning.png'
        })
      },
      complete: () => {
        this.setData({
          loading: 0
        })
      }
    })
  },

  // 查询所有已签到的会议
  getSignInRecord() {
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getUserCheckIn',
      method: 'POST',
      data: {
        manager: app.globalData.userId
      },
      dataType: 'json',
      success: res => {
        if (res.data.data) {
          this.setData({
            meetingList: res.data.data
          })
        }
      },
      fail: res => {
        wx.showToast({
          title: '网络异常！',
          image: '/images/warning.png'
        })
      },
      complete: () => {
        this.setData({
          loading: 0
        })
      }
    })
  },

  toOneMeeting(e) {
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: `/pages/signInSetting/signInSetting?meetingId=${e.currentTarget.dataset.meetingid}&type=${e.currentTarget.dataset.type}&name=${e.currentTarget.dataset.name}`,
    })
  },

  existMeeting(e) {
    wx.setStorageSync("signInQr", this.data.meetingList[e.currentTarget.dataset.index])
    wx.navigateTo({
      url: '/pages/signInRegister/signInRegister?type=from exist',
    })
  },

  bindChange: util.throttle(function(e) {
    console.log(e)
    this.setData({
      loading: 1,
      currentTab: e.detail.current
    })
    if (this.data.currentTab == 0) {
      this.getMeetingList()
    } else {
      this.getSignInRecord()
    }
  }, 300),

  swichNav: function(e) {
    this.setData({
      loading: 1,
      currentTab: e.currentTarget.dataset.current
    })
    if (this.data.currentTab == 0) {
      this.getMeetingList()
    } else {
      this.getSignInRecord()
    }
  },
})